<?php


namespace app\admin\controller;


use app\constant\BillConst;
use app\constant\WithdrawalsConst;
use app\model\AppUserBill;
use app\model\AppUserWallet;
use app\model\AppWithdrawals;
use app\util\ReturnCode;
use Exception;
use think\Db;
use think\exception\DbException;

class Withdrawals extends Base
{
    //获取提现记录
    public function getList()
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $status = $this->request->get('status', null);
        $where = [];
        if (isset($status)) {
            $where['status'] = $status;
        }
        $where = $this->addTid($where);
        $obj = new AppWithdrawals();
        try {
            $listObj = $obj->where($where)->order('create_time DESC')->paginate($limit, false, ['page' => $start])->toArray();
            return $this->buildSuccess([
                'list' => $listObj['data'],
                'count' => $listObj['total']
            ]);
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::FAILED, '获取失败');
    }
    //标记为完成
    //先修改提现记录，然后添加到bill中
    public function finish()
    {
        $id = $this->request->get('id');
        Db::startTrans();
        try {
            $where = ['id' => $id,'status'=>WithdrawalsConst::STATUS_UN_OP];
            $where = $this->addTid($where);
            $model = AppWithdrawals::get($where);
            if (!$model) {
                return $this->buildFailed(ReturnCode::FAILED, '不存在的记录');
            }
            $model->setAttr('status', WithdrawalsConst::STATUS_FINISH);
            $model->setAttr('op_uid', $this->userInfo['id']);
            $done = $model->allowField(true)->save();
            if (!$done) {
                throw new Exception('修改提现记录失败');
            }
            $userWhere = ['uid' => $model->uid, 'type' => BillConst::TYPE_EXPENDITURE, 'money' => $model->money, 'source' => BillConst::COST_TYPE_WITHDRAWALS, 'desc' => '提现'];
            $userWhere = $this->addTid($userWhere);
            $done = AppUserBill::create($userWhere);
            if (!$done) {
                throw new Exception('创建明细失败');
            }
            Db::commit();
            return $this->buildSuccess(null);
        } catch (Exception $e) {
            Db::rollback();
        }
        return $this->buildFailed(ReturnCode::FAILED, '操作失败');
    }
    //标记为关闭
    //先修改提现记录，然后恢复余额
    public function close()
    {
        $id = $this->request->get('id');
        Db::startTrans();
        try {
            $where = ['id' => $id,'status'=>WithdrawalsConst::STATUS_UN_OP];
            $where = $this->addTid($where);
            $model = AppWithdrawals::get($where);
            if (!$model) {
                return $this->buildFailed(ReturnCode::FAILED, '不存在的记录');
            }
            $model->setAttr('status', WithdrawalsConst::STATUS_CLOSE);
            $model->setAttr('op_uid', $this->userInfo['id']);
            $done = $model->allowField(true)->save();
            if (!$done) {
                throw new Exception('修改提现记录失败');
            }
            $userWhere = ['uid' => $model->uid];
            $userWhere = $this->addTid($userWhere);
            $done = (new AppUserWallet())->where($userWhere)->setInc('income', $model->money);
            if (!$done) {
                throw new Exception('恢复余额失败');
            }
            Db::commit();
            return $this->buildSuccess(null);
        } catch (Exception $e) {
            Db::rollback();
        }
        return $this->buildFailed(ReturnCode::FAILED, '操作失败');
    }
}